package services.zpcj;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import vo.system.AZ02;
import vo.system.AZ09;
import com.Const;
import com.PublicSystem;
import com.util.Util;
/**
* 照片采集
* @author bjh
* @version Fri May 06 17:59:17 CST 2016 gjx
* @since 1.0
*/
public class ZpcjServices {
	PublicSystem sys = PublicSystem.getInstance();
	/**
	 * 获取信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public HashMap getUnitData(String id) throws Exception {		
		String sql = "select aac001,aac004, aac058, aac005, aac003, aac002, aae005, aac006 from ac01 where AAC001 = ? ";		
		List data = sys.query(sql, new Object[] {id}, true);	
		HashMap map = new HashMap();
		if(data.size() > 0) {
		    map = (HashMap)data.get(0);
			String sql2 ="select b.aab001, b.aab004 from aca1 a, ab01 b where a.aab001 = b.aab001 and a.AAC001 = ?";
			List data2 = sys.query(sql2, new Object[] {id}, true);
			if(data2.size() > 0 ) { 
				HashMap obj = (HashMap)data2.get(0);
				map.put("aab001", obj.get("aab001"));
				map.put("aab004", obj.get("aab004"));
			}
		}
		else {
			return null;
		}
		return map;
	}
	/**
	 * 下拉框选项
	 * @param lx
	 * @return
	 * @throws Exception
	 */
	public List getSelect(String lx) throws Exception {
		String sql = "select aaa103, aaa102 FROM aa10 where aaa100 = ?";
		List data = sys.query(sql, new Object[] {lx}, true);
		return data;
	}
	/**
	 * 查询单位名称
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public String checkuint(String id) throws Exception {		
		String checksql = "select aab004 from ab01 where aab620='0' and aab001 = '" + id + "'";
		String res = sys.SelResultStr(checksql);
		return res;	
	}
	/**
	 * 查询信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public String getEventid(HashMap obj) throws Exception {		
		String[] str = sys.callFunction("ZJ_GET_EVENTID", obj).split("&");
		String eventid = str[0];
		return eventid;	
	}
	/**
	 * 校验批量采集
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public HashMap chkzpplcj(HashMap obj) throws Exception {		
		String[] str = sys.callDBFuncProce("ic_az20.chk_zpplcj", "function", obj).split("&");
		String eventid = obj.get("eventid").toString();			
		String sql1 = "select count(0) from zj_tmp where EVENTID= '" + eventid + "' and EXECMSG is null";
		String sql2 = "select count(0) from zj_tmp where EVENTID= '" + eventid + "' and EXECMSG is not null";
		String sum1 = sys.SelResultStr(sql1).toString();
		String sum2 = sys.SelResultStr(sql2).toString();
		HashMap map = new HashMap();
		map.put("sum1", sum1);
		map.put("sum2", sum2);
		return map;	
	}
	/**
	 * 查询照片信息
	 * @param id
	 * @param response
	 * @throws Exception
	 */
	public String check(String id) throws Exception {		
		String checksql = "select aac001 from az02 where aac001 ='" + id + "'";
		String res = sys.SelResultStr(checksql);
		if(null == res) {
			return "0";
		} else {
			return "1";
		}
		
	}
	/**
	 * 查询社会保障卡号
	 * @param sfz
	 * @param response
	 * @throws Exception
	 */
	public String getSfz(String sfz) throws Exception {		
		String checksql = "select aac001 from ac01 where aac002 ='" + sfz + "'";
		String res = sys.SelResultStr(checksql);
		if(null == res) {
			return "0";
		} else {
			return res;
		}
		
	}
	/**
	 * 插入照片
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public int insertUnit(HashMap obj1, String id, HashMap obj2, String localFile) throws Exception {	 			
	 	AZ02 vo = new AZ02();	
	 	AZ09 voo = new AZ09();
	 	Date now = new Date();
	 	int num = 1;
		vo=(AZ02)Util.MapToVo(vo, obj1);
		voo=(AZ09)Util.MapToVo(voo, obj2);
		voo.setAZE001(now);
		voo.setAAZ753(now);
		voo.setAAZ718(now);
		try {		
			sys.BeginTransaction();
			sys.Jdbc_saveDaoTransaction(vo);
			sys.Jdbc_saveDaoTransaction(voo);			
			String sql = "update az02 set aac100 =? where aac001 ='" + id + "'";
			sys.excuteBlobTransaction(sql, localFile);		
			sys.endTransaction();
		} catch (Exception d) {		
			num=0;
		}
		return num; 
	} 
	/**
	 * 照片入库
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public String insertplUnit(HashMap obj) throws Exception {	 			
	 	AZ02 vo = new AZ02();		
		vo = (AZ02)Util.MapToVo(vo, obj);		
		sys.Jdbc_saveDao(vo);	
		return "";
	} 
	/**
	 * 查询正确信息
	 * @param obj
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	public HashMap getsuccess(Object[] obj, HashMap parms) throws Exception {
		String sql = "select * from v_az02pl where EVENTID= ? and EXECMSG is null" ;			
		return sys.getGrid(sql, obj, parms, true);
	}
	/**
	 * 查询错误信息
	 * @param obj
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	public HashMap getfail(Object[] obj, HashMap parms) throws Exception {
		String sql = "select * from v_az02pl where EVENTID= ? and EXECMSG is not null" ;			
		return sys.getGrid(sql, obj, parms, true);
	}
	/**
	 * 获取社保卡号
	 * @param lx
	 * @return
	 * @throws Exception
	 */
	public List<String> getAcc001s(String id) throws Exception {
		String sql = "select c1 from zj_tmp where EVENTID = '" + id + "' and EXECMSG is null";
		List data = sys.SelResultList(sql);
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < data.size(); i++) {
			HashMap map = (HashMap) data.get(i);
			String c1 = map.get("c1").toString(); 
			list.add(c1);
		}
		return list;
	}
	/**
	 * 数据导出下载
	 * @author duy
	 * @param obj
	 * @param parms
	 * @param response 
	 * @throws Exception
	 */
	public File exkcdate(String id) throws Exception {
		String sql = "select aac001, aac003, aac002, aac004, aac005, execmsg from v_az02pl where EXECMSG is not null and EVENTID = '" + id + "'";
		File f = new File(Const.server_path + "/temp/excel.xls");
		//如果父目录不在就创建
		if(!f.getParentFile().exists()) {
			f.getParentFile().mkdirs();
		}	
		if((!f.exists())) {
			f.createNewFile();
		}		
		sys.writeExcelTitle(new FileOutputStream(f), sql, new String[] {
				"社会保障卡号", "姓名", "证件号", "性别", "民族", "错误信息"});
		return f;
	}
}
